package com.inossem.warehousestorage_inossem.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;

import com.inossem.utils.base.QRCodeUtils;
import com.inossem.utils.base.Utils;
import com.inossem.utils.constant.QRCodeConstant;
import com.inossem.utils.log.LogUtils;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.constant.ReceiverConstant;

/**
 * Created by leij on 2017/9/27.
 */

public class ScanReceiver extends BroadcastReceiver {

    private static final String INFO = "info:";

    private static final String TYPE = "type:";

    private static final String ERROR_MESSAGE = "errorMessage:";

    private static final String REALINFO = "realInfo:";

    private static final String QRCODE = "【QRCode】";

    private ScanListener mScanListener;

    private String[] mQRCodeTypeArray;

    public ScanReceiver(ScanListener scanListener, String... qrCodeTypeArray) {
        mScanListener = scanListener;
        mQRCodeTypeArray = qrCodeTypeArray;
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(ReceiverConstant.SCAN_ACTION)) {
            String value = TextUtils.isEmpty(intent.getStringExtra(ReceiverConstant.SCAN_EXTRA_VALUE)) ? "" : intent.getStringExtra(ReceiverConstant.SCAN_EXTRA_VALUE);
//            int length = intent.getIntExtra(ReceiverConstant.SCAN_EXTRA_LENGTH, 0);
            if (mScanListener != null) {
                String illegalMessage = QRCodeUtils.checkQRCode(context, value);
                if (illegalMessage == null) {
                    if (QRCodeUtils.checkQRCodeType(value, mQRCodeTypeArray)) {
                        String result = value.substring(0, value.length() - QRCodeConstant.QRCODE_SECURITY.length());
                        successLog(context, value, result);
                        mScanListener.onResult(result, result.length());
                    } else {
                        errorLog(context, value, context.getResources().getString(R.string.utils_qrcode_type_wrong));
                        Utils.middleToast(context, context.getResources().getString(R.string.utils_qrcode_type_wrong));
                    }
                } else {
                    errorLog(context, value, illegalMessage);
                    Utils.middleToast(context, illegalMessage);
                }
            }
        }
    }

    private void successLog(Context context, String value, String result) {
        String type = result.substring(0, QRCodeConstant.QRCODE_TYPE_LENGTH);
        String realInfo = result.substring(QRCodeConstant.QRCODE_TYPE_LENGTH, result.length());
        LogUtils.logQRCodeI(context, QRCODE + "\n" + INFO + value + "\n" + TYPE + type + "\n" + REALINFO + realInfo);
    }

    private void errorLog(Context context, String value, String result) {
        LogUtils.logQRCodeI(context, QRCODE + "\n" + INFO + value + "\n" + ERROR_MESSAGE + result);
    }

    public interface ScanListener {
        void onResult(String scanResult, int length);
    }


}
